<?php

class modul {
	protected $modDir;
	protected $modul;
	protected $action;
	protected $avActions;
	
	protected $errorCode;
	protected $errReport;
	
	public function __construct($dir='./modules/', $err=false) {
		if(substr($dir, -1, 1) != '/') {
			$dir.='/';
		}
		$this->modDir = $dir;
		
		if(!is_dir($this->modDir)) {
			if($this->errReport) {
				$this->error('Selected modul dictonary does not exist: '.$this->modDir);
			}
			$this->Set_Error_Code(0);
			return false;
		}
		
		if($err) {
			$this->errReport = true;
		}
	}

	private function Find_Suffix($file) {
		$fileParts = explode('.', $file);
		$lastPart = $fileParts[count($fileParts)-1];
		return strtolower($lastPart);
	}	
	private function Find_Path_Parts($file) {
		$pathParts = explode('/', $file);
		$ret['name'] = $pathParts[count($pathParts)-1];
		unset($pathParts[count($pathParts)-1]);
		$ret['path'] = implode('/', $pathParts);
		return $ret;
	}
	
	private function Select_Modul($mod) {
		$this->modul = $mod;
	}
	
	private function Select_Action($action) {
		$this->action = $action;
	}
	
	/**
	 * Modul laden
	 * @param object $modul Modulbezeichnung; default = index
	 * @return boolean
	 */
	public function load($modul='index') {
		if(empty($this->modul)) {
			$this->Select_Modul($modul);
		}
		
		$modulDir = $this->modDir.$this->modul.'/';
		if(is_dir($modulDir)) {
			$open = opendir($modulDir);
			$n=0;
			while($read = readdir($open)) {
				if(filetype($modulDir.$read) == 'file' && $this->Find_Suffix($read) == 'php') {
					$this->avActions[$read] = $modulDir.$read;
					$n++;
				}
			}
			
			if(!$n) {
				if($this->errReport) {
					$this->error('There aren\'t any actions in modul: '.$this->modul.' | '.$modulDir);
				}
				$this->Set_Error_Code(4);
				return false;
			}
		}
		else {
			if($this->errReport) {
				$this->error('Modul does not exist: '.$this->modul.' | '.$modulDir);
			}
			$this->Set_Error_Code(1);
			return false;
		}
	}
	
	/**
	 * Aktion laden und ausführen
	 * @param object $action Bezeichnung der Aktion
	 * @param object $var Eine Variable, die an die Aktion übergeben wird (string, array, object)
	 * @return string
	 */
	public function action($action='index', $var=false) {
		if(empty($this->action)) {
			$this->Select_Action($action);
		}
		if(!empty($this->avActions)) {
			if(isset($this->avActions[$this->action.'.php'])) {
				require_once $this->avActions[$this->action.'.php'];
				$fnname = $this->action;
				if(function_exists($fnname)) {
					$ret = $fnname($var);
					return $ret;
				}
				elseif(function_exists('__construct')) {
					$ret = __construct($var);
					return $ret;
				}
				else {
					if($this->errReport) {
						$this->error('Not valid action function: '.$this->action);
					}
					$this->Set_Error_Code(3);
					return false;
					return false;
				}
			}
			else {
				if($this->errReport) {
					$this->error('Selected Action does not exist or isn\'t available: '.$this->action);
				}
				$this->Set_Error_Code(2);
				return false;
			}
		}
	}
	
	private function Set_Error_Code($code) {
		$this->errorCode[] = $code;
	}

	private function error($message) {
		echo "<div style=\"padding: 0 10px; border: 2px solid #eee;\">";
		echo "<pre>";
		echo "## topwork | Class: modul ##\n\n";
		echo "-> ".$message;
		echo "</pre>";
		echo "</div>";
	}
}
